package com.net.oil.car;

import android.content.Intent;
import android.os.Bundle;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.car.adapter.CarBrandRecyclerViewAdapter;
import com.net.oil.car.bean.CarBrandBean;
import com.net.oil.car.contract.SelectCarBrandContract;
import com.net.oil.car.presenter.SelectCarBrandPresenter;
import com.net.oil.widget.SideBarView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectCarBrandActivity extends BaseActivity<SelectCarBrandPresenter> implements SelectCarBrandContract.View {

    @BindView(R.id.side_bar)
    SideBarView sideBar;
    @BindView(R.id.car_brand_recycler_view)
    RecyclerView carBrandRecyclerView;
    @BindView(R.id.tip)
    TextView tip;

    private CarBrandRecyclerViewAdapter adapter;
    private LinearLayoutManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_car_brand);
        ButterKnife.bind(this);
        presenter = new SelectCarBrandPresenter(this);
        initView();
        initData();
    }

    private void initData() {
        showProgressDialog();
        presenter.getCarBrand();

    }

    private void initView() {

        adapter = new CarBrandRecyclerViewAdapter(this);
        carBrandRecyclerView.setAdapter(adapter);
        adapter.setOnCarBrandItemClickListener(new CarBrandRecyclerViewAdapter.OnCarBrandItemClickListener() {
            @Override
            public void onClick(int carBrandId) {
                Intent intent = new Intent(SelectCarBrandActivity.this, CarSeriesActivity.class);
                intent.putExtra(Constant.INTENT_CAR_BRAND_ID, carBrandId);
                startActivity(intent);
                finish();
            }
        });


        manager = new LinearLayoutManager(this);
        carBrandRecyclerView.setLayoutManager(manager);
        carBrandRecyclerView.addItemDecoration(new DividerItemDecoration(this, manager.getOrientation()));
        sideBar.setSideBarLetterSelectListener(new SideBarView.OnChooseLetterChangedListener() {
            @Override
            public void onChooseLetter(String s) {
                int i = adapter.getPositionForSection(s);
                if (i == -1) {
                    return;
                }
                manager.scrollToPositionWithOffset(i, 0);
                tip.setVisibility(View.VISIBLE);
                tip.setText(s);
            }

            @Override
            public void onNoChooseLetter() {
                tip.setVisibility(View.GONE);
            }
        });
    }

    /**
     * 获取车辆品牌列表成功
     * @param carBrandBeans 车辆品牌列表
     */
    @Override
    public void getCarBrandSuccess(List<CarBrandBean> carBrandBeans) {
        dismissProgressDialog();
        if (carBrandBeans != null){
            adapter.updateData(carBrandBeans);
        }
    }

    /**
     * 获取车辆品牌列表失败
     */
    @Override
    public void getCarBrandFailed() {
        dismissProgressDialog();
    }

}
